package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic.PdtClassDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic.ProductDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtClassExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtInfExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CAshGateVideoBindServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetProductVo;
import com.xiaoxu.intranetweb.util.IllegalCharacterCheck;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * 品种管理业务层
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2023/1/13 15:00
 * @since 1.0
 */
@Component
public class

ProductBu {
    @Autowired
    private ICProductsInfoService iCProductsInfoService;
    @Autowired
    private ICPdtClassService iCPdtClassService;
    @Autowired
    private ICCompanysService companysService;
    @Autowired
    private CAshGateVideoBindServiceImpl cAshGateVideoBindService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ISysUserPlantConnService userPlantConnService;
    @Autowired
    private ProductDic productDic;
    @Autowired
    private PdtClassDic pdtClassDic;
    @Autowired
    private ICPdtsAuthService iCPdtsAuthService;
    @Autowired
    private ICWeightSheetsService iCWeightSheetsService;
    @Autowired
    private ICIcCardsInfoService iCIcCardsInfoService;
    @Autowired
    private ICAshGateVideoBindService  iCAshGateVideoBindService;
    @Autowired
    private ICOrderDetailService iCOrderDetailService;

    //查询出所有品种
    public SzResult getProduct(String value, String pdtClass, Integer stockCls, String token, String plantId) throws Exception {
//c_products_info表
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
//        拿到所有值
        queryWrapper.eq(value != null && !value.isEmpty(), "ID", value);
        queryWrapper.eq(pdtClass != null, "PDTINF_CLASS", pdtClass);
        queryWrapper.eq(stockCls != null, "PDTINF_STOCKCLS", stockCls);
        queryWrapper.eq(plantId != null && !plantId.isEmpty(), "DATA_FROM", plantId);
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.orderByDesc("PDTINF_ID");
        List<CProductsInfo> cProductsInfoList = iCProductsInfoService.list(queryWrapper);
        List<GetProductVo> getProductVoList = new ArrayList<>();
        for (CProductsInfo cProductsInfo : cProductsInfoList) {
            GetProductVo getProductVo = new GetProductVo();
            getProductVo.setId(cProductsInfo.getId());
            getProductVo.setPdtinfId(String.valueOf(cProductsInfo.getPdtinfId()));
            getProductVo.setPdtinfSn(cProductsInfo.getPdtinfSn());
            getProductVo.setPdtinfClass(cProductsInfo.getPdtinfClass());
            if (cProductsInfo.getPdtinfClass() != null) {
                QueryWrapper<CPdtClass> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cProductsInfo.getPdtinfClass());
                queryWrapper1.eq("PDTCLASS_DELETED", 0);
                CPdtClass cPdtClass = iCPdtClassService.getOne(queryWrapper1);
                if (cPdtClass !=null) {
                    getProductVo.setPdtinfPdtClassName(cPdtClass.getPdtclassClass());
                }else {
                    getProductVo.setPdtinfPdtClassName("");
                }
            } else {
                getProductVo.setPdtinfPdtClassName("");
            }
            getProductVo.setPdtinfPdt(cProductsInfo.getPdtinfPdt());
            getProductVo.setPdtinfBundPdt(cProductsInfo.getPdtinfBundPdt());
            getProductVo.setPdtinfAlias(cProductsInfo.getPdtinfAlias());
            getProductVo.setPdtinfPrice(cProductsInfo.getPdtinfPrice());
            //品种容量
            if (cProductsInfo.getPdtinfCapacity() != null) {
                getProductVo.setPdtinfCapacity(cProductsInfo.getPdtinfCapacity());
            }else {
                getProductVo.setPdtinfCapacity(-1);
            }
            getProductVo.setPdtinfSubjectId(cProductsInfo.getPdtinfSubjectId());
            if (cProductsInfo.getPdtinfSubjectId() != null) {
                QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cProductsInfo.getPdtinfSubjectId());
                CCompanys cCompanys = companysService.getOne(queryWrapper2);
                if (cCompanys != null) {
                    getProductVo.setPdtinfSubjectName(cCompanys.getCpyCo());
                    getProductVo.setPdtinfSubjectChiName(cCompanys.getCpyChico());
                }else {
                    getProductVo.setPdtinfSubjectName("");
                    getProductVo.setPdtinfSubjectChiName("");
                }
            } else {
                getProductVo.setPdtinfSubjectName("");
                getProductVo.setPdtinfSubjectChiName("");
            }
//            getProductVo.setPdtinfSpeSitelist(cProductsInfo.getPdtinfSpeSitelist());
            if (cProductsInfo.getPdtinfSpeSitelist() != null && !cProductsInfo.getPdtinfSpeSitelist().equals("") && !cProductsInfo.getPdtinfSpeSitelist().equals("[]")) {

                String[] pdtinfSpeSitelist = cProductsInfo.getPdtinfSpeSitelist().replace("\"", "").replace("[", "").replace("]", "").split(",");
                List<String> pdtinfSpeSitelist1 = Arrays.stream(pdtinfSpeSitelist).map(String::trim).collect(Collectors.toList());
                getProductVo.setPdtinfSpeSitename(pdtinfSpeSitelist1);
            } else {
                getProductVo.setPdtinfSpeSitename(null);
            }
            if (cProductsInfo.getPdtinfSpeSitelist() != null && !cProductsInfo.getPdtinfSpeSitelist().trim().isEmpty()) {
                // 分割并清理字符串，直接转换为列表
                List<String> ashGateNames = Arrays.asList(cProductsInfo.getPdtinfSpeSitelist().split(",")).stream()
                        .map(String::trim) // 清理前后空白
                        .collect(Collectors.toList());

                // 遍历名称列表，查询每个装载点的ID
                List<String> pdtinfSpeSitelistIds = new ArrayList<>();
                for (String ashGateName : ashGateNames) {
                    // 准备查询条件，这部分只需设置一次即可
                    QueryWrapper<CAshGateVideoBind> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("DATA_FROM", plantId);
//                    queryWrapper2.ne("ASHGATE_DELETED", 1);
//                    queryWrapper2.isNull("ASHGATE_DELETED");
                    queryWrapper2.and(i->i.ne("ASHGATE_DELETED",1).or().isNull("ASHGATE_DELETED"));
//                    queryWrapper2.clear();
                    queryWrapper2.eq("ASHGATE_NAME", ashGateName); // 动态设置装载点名称条件
                    CAshGateVideoBind ashGateVideoBind = cAshGateVideoBindService.getOne(queryWrapper2);
                    if (ashGateVideoBind != null) {
                        pdtinfSpeSitelistIds.add(ashGateVideoBind.getId());
                    }
                }
                // 设置查询结果到 getProductVo
                getProductVo.setPdtinfSpeSitelist(pdtinfSpeSitelistIds.isEmpty() ? null : pdtinfSpeSitelistIds);
            } else {
                getProductVo.setPdtinfSpeSitelist(null);
            }
            if (cProductsInfo.getPdtinfSpeSnlist() != null && !cProductsInfo.getPdtinfSpeSnlist().equals("") && !cProductsInfo.getPdtinfSpeSnlist().equals("[]")) {
                List<String> pdtinfSpeSnlist = Arrays.stream(cProductsInfo.getPdtinfSpeSnlist().replace("\"", "").replace("[", "").replace("]", "").split(",")).map(String::trim).collect(Collectors.toList());
                getProductVo.setPdtinfSpeSnlist(pdtinfSpeSnlist);
            } else {
                getProductVo.setPdtinfSpeSnlist(null);
            }
            if (cProductsInfo.getPdtinfSpeGateslist() != null && !cProductsInfo.getPdtinfSpeGateslist().equals("") && !cProductsInfo.getPdtinfSpeGateslist().equals("[]")) {
                List<String> pdtinfSpeGateslist = Arrays.stream(cProductsInfo.getPdtinfSpeGateslist().replace("\"", "").replace("[", "").replace("]", "").split(",")).map(String::trim).collect(Collectors.toList());
                getProductVo.setPdtinfSpeGateslist(pdtinfSpeGateslist);
            } else {
                getProductVo.setPdtinfSpeGateslist(null);
            }
            if (cProductsInfo.getPdtinfSpePleaseoncelist() != null && !cProductsInfo.getPdtinfSpePleaseoncelist().equals("") && !cProductsInfo.getPdtinfSpePleaseoncelist().equals("[]")) {
                List<String> pdtinfSpePleaseoncelist = Arrays.stream(cProductsInfo.getPdtinfSpePleaseoncelist().replace("\"", "").replace("[", "").replace("]", "").split(",")).map(String::trim).collect(Collectors.toList());
                getProductVo.setPdtinfSpePleaseoncelist(pdtinfSpePleaseoncelist);
            } else {
                getProductVo.setPdtinfSpePleaseoncelist(null);
            }
            if (cProductsInfo.getPdtinfSpeReadylist() != null && !cProductsInfo.getPdtinfSpeReadylist().equals("") && !cProductsInfo.getPdtinfSpeReadylist().equals("[]")) {
                List<String> pdtinfSpeReadylist = Arrays.stream(cProductsInfo.getPdtinfSpeReadylist().replace("\"", "").replace("[", "").replace("]", "").split(",")).map(String::trim).collect(Collectors.toList());
                getProductVo.setPdtinfSpeReadylist(pdtinfSpeReadylist);
            } else {
                getProductVo.setPdtinfSpeReadylist(null);
            }
            getProductVo.setSvrTmstamp(cProductsInfo.getSvrTmstamp());
            getProductVo.setCliTmstamp(cProductsInfo.getCliTmstamp());
            getProductVo.setPdtinfLoadCtrl(cProductsInfo.getPdtinfLoadCtrl());
            getProductVo.setPdtinfIdentyLoad(cProductsInfo.getPdtinfIdentyLoad());
            getProductVo.setPdtinfPlanCtrl(cProductsInfo.getPdtinfPlanCtrl());
            getProductVo.setPdtinfQueue(cProductsInfo.getPdtinfQueue());
            getProductVo.setPdtinfIdentyQueue(cProductsInfo.getPdtinfIdentyQueue());
            getProductVo.setPdtinfStockcls(cProductsInfo.getPdtinfStockcls());
            getProductVo.setPdtinfDeleted(cProductsInfo.getPdtinfDeleted());
            getProductVo.setPdtinfNew(cProductsInfo.getPdtinfNew());
            getProductVo.setDownedID1(cProductsInfo.getDownedID1());
            getProductVo.setDownedID2(cProductsInfo.getDownedID2());
            getProductVo.setDownedID3(cProductsInfo.getDownedID3());
            getProductVo.setDownedID4(cProductsInfo.getDownedID4());
            getProductVo.setDownedID5(cProductsInfo.getDownedID5());
            getProductVo.setDownedID6(cProductsInfo.getDownedID6());
            getProductVo.setDownedID7(cProductsInfo.getDownedID7());
            getProductVo.setDownedID8(cProductsInfo.getDownedID8());
            getProductVo.setDownedID9(cProductsInfo.getDownedID9());
            getProductVo.setDownedID10(cProductsInfo.getDownedID10());
            getProductVo.setDownedID11(cProductsInfo.getDownedID11());
            getProductVo.setDownedID12(cProductsInfo.getDownedID12());
            getProductVo.setDownedID13(cProductsInfo.getDownedID13());
            getProductVo.setDownedID14(cProductsInfo.getDownedID14());
            getProductVo.setDownedID15(cProductsInfo.getDownedID15());
            getProductVo.setDownedID16(cProductsInfo.getDownedID16());
            getProductVo.setDownedID17(cProductsInfo.getDownedID17());
            getProductVo.setDownedID18(cProductsInfo.getDownedID18());
            getProductVo.setDownedID19(cProductsInfo.getDownedID19());
            getProductVo.setDownedID20(cProductsInfo.getDownedID20());
            getProductVo.setCompeteFlag(cProductsInfo.getCompeteFlag());
            getProductVo.setDataFrom(cProductsInfo.getDataFrom());
//            getProductVo.setSpeloadsitepdtList(cProductsInfo.getSpeloadsitepdtList());
            getProductVo.setPdtinfSpeLoadsite(cProductsInfo.getPdtinfSpeLoadsite());
            getProductVoList.add(getProductVo);
        }
        return SzResult.success(getProductVoList);

    }
    public SzResult getSaleProduct(String plantId) {
        QueryWrapper<CProductsInfo> productsInfoQueryWrapper = new QueryWrapper<>();
        productsInfoQueryWrapper.ne("ID","9e5687146b894a8b840533d4ed5aceb3");//采购物资
        productsInfoQueryWrapper.ne("ID","4");//其他物资
        productsInfoQueryWrapper.eq("DATA_FROM",plantId);
        productsInfoQueryWrapper.eq("PDTINF_DELETED",0);
        List<Map<String, Object>> list = iCProductsInfoService.list(productsInfoQueryWrapper).stream().map(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", item.getId());
            map.put("pdtinfPdt", item.getPdtinfPdt());
            return map;
        }).collect(Collectors.toList());
        return SzResult.success(list);
    }

    //新增产品
    public SzResult addProduct(String pdtInfClass, String pdtInfPdt, Integer pdtInfPrice, List<String> pdtinfSpeSitelist, String pdtinfSpeSnlist, String pdtinfSpeGateslist, String affiliatedUnit,
                               String pdtInfSpeReadyList, String pdtInfSpePleaseOnCelist, Integer pdtInfStockcls,Integer pdtinfSn, Integer pdtinfCapacity ,String token, String plantId) throws Exception {
      if (pdtInfStockcls !=1) {
          if (IllegalCharacterCheck.isNameLawful(pdtInfPdt)) {
              return SzResult.error("品种名里有非法字符");
          }
      }else {
          if (IllegalCharacterCheck.isNameLawful2(pdtInfClass)) {
              return SzResult.error("产品类型里有非法字符");
          }
      }
        CProductsInfo productsInfo = new CProductsInfo();
        if (pdtInfClass == null || pdtInfClass.equals("")) {
            return SzResult.error("产品类型不能为空");
        }
        if (pdtInfPdt == null || pdtInfPdt.equals("")) {
            return SzResult.error("产品名称不能为空");
        }
        if (pdtInfPrice == null) {
            return SzResult.error("产品价格不能为空");
        }
        if (affiliatedUnit == null) {
            return SzResult.error("产品所属单位不能为空");
        }
        if (pdtinfSn != null) {
           productsInfo.setPdtinfSn(new BigDecimal(pdtinfSn));
        }
        productsInfo.setPdtinfCapacity(pdtinfCapacity);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        QueryWrapper<CProductsInfo> maxIdQueryWrapper = new QueryWrapper<>();
        maxIdQueryWrapper.orderByDesc("PDTINF_ID");
        maxIdQueryWrapper.last("limit 1");
        maxIdQueryWrapper.eq("DATA_FROM", plantId);
        CProductsInfo productsInfo1 = iCProductsInfoService.getOne(maxIdQueryWrapper);
        if (productsInfo1 != null) {
            BigDecimal productsInfoId = productsInfo1.getPdtinfId();
            String pdtsauthId1 = String.valueOf((productsInfoId.intValue() + 1));
            productsInfo.setPdtinfId(new BigDecimal(pdtsauthId1));
            if (pdtinfSn == null) {
                productsInfo.setPdtinfSn(new BigDecimal(pdtsauthId1));
            }
        } else {
            productsInfo.setPdtinfId(new BigDecimal(1));
            if (pdtinfSn == null) {
                productsInfo.setPdtinfSn(new BigDecimal(1));
            }
        }
        productsInfo.setId(uuid);
        productsInfo.setPdtinfClass(pdtInfClass);
        //拿到C_products_info表的最大id
        QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.orderByDesc("PDTINF_ID");
        queryWrapper2.last("limit 1");
        queryWrapper2.isNotNull("PDTINF_ID");
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper2);
        if (cProductsInfo == null || cProductsInfo.getPdtinfId() ==null) {
            productsInfo.setPdtinfId(BigDecimal.valueOf(1));
        } else {
            BigDecimal pdtinfId = cProductsInfo.getPdtinfId();
            BigDecimal add = pdtinfId.add(BigDecimal.valueOf(1));
            productsInfo.setPdtinfId(BigDecimal.valueOf(Long.parseLong(add.toString())));
        }
        //pdtInfPdt不能重复
        QueryWrapper<CProductsInfo> queryWrapper3 = new QueryWrapper<>();
        if (pdtInfStockcls == 1) {//进货类
            queryWrapper3.eq("PDTINF_PDT", "." + pdtInfPdt);
        } else {
            queryWrapper3.eq("PDTINF_PDT", pdtInfPdt);
        }
//        queryWrapper3.eq("PDTINF_PDT", pdtInfPdt);
        queryWrapper3.eq("PDTINF_DELETED", 0);
        CProductsInfo cProductsInfo1 = iCProductsInfoService.getOne(queryWrapper3);
        if (cProductsInfo1 != null) {
            return SzResult.error("产品名称不能重复");
        }
        if (pdtInfStockcls == 1) {//进货类
            productsInfo.setPdtinfPdt("." + pdtInfPdt);
//            进货类要加余量数据
            String uuid1 =  java.util.UUID.randomUUID().toString().replace("-", "");
            QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ID", affiliatedUnit);
            queryWrapper.eq("CPY_DELETED", 0);
//        queryWrapper.eq("CPY_PASS", 0);
            CCompanys cCompanys = companysService.getOne(queryWrapper);
            COrderDetail cOrderDetail;
            QueryWrapper<COrderDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ORDETAIL_CO", cCompanys.getCpyCo());
            queryWrapper1.eq("ORDETAIL_CHICO", cCompanys.getCpyChico());
            queryWrapper1.eq("ORDETAIL_PDT", "." + pdtInfPdt);
            queryWrapper1.eq("ORDETAIL_DELETED", "." + pdtInfPdt);
            cOrderDetail = iCOrderDetailService.getOne(queryWrapper1);
            //  String plantId = plantId;
            if (cOrderDetail == null) {//没有这个账户

                //新增账户
                COrderDetail cOrderDetail1 = new COrderDetail();
                cOrderDetail1.setId(uuid1);
//                cOrderDetail1.setOrdetailId(uuid);
                QueryWrapper<COrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
                orderDetailQueryWrapper.orderByDesc("ORDETAIL_ID");
                orderDetailQueryWrapper.last("limit 1");
                orderDetailQueryWrapper.eq("DATA_FROM", plantId);
                COrderDetail cOrderDetail2 = iCOrderDetailService.getOne(orderDetailQueryWrapper);
                if (cOrderDetail2 != null) {
                    BigDecimal ordetailId = cOrderDetail2.getOrdetailId();
                    String ordetailId1 = String.valueOf((ordetailId.intValue() + 1));
                    cOrderDetail1.setOrdetailId(new BigDecimal(ordetailId1));
                } else {
                    cOrderDetail1.setOrdetailId(new BigDecimal(1));
                }
                cOrderDetail1.setOrdetailPdt("." + pdtInfPdt);
                cOrderDetail1.setOrdetailItemNum(BigDecimal.valueOf(0));
                cOrderDetail1.setOrdetailRemainNum(BigDecimal.valueOf(0));
                cOrderDetail1.setOrdetailLastinNum(BigDecimal.valueOf(0));
                cOrderDetail1.setOrdetailCo(cCompanys.getCpyCo());
                cOrderDetail1.setOrdetailChico(cCompanys.getCpyChico());
                cOrderDetail1.setOrdetailModifyTime(LocalDateTime.now());
                cOrderDetail1.setDataFrom(plantId);
                cOrderDetail1.setOrdetailDeleted(0);
                cOrderDetail1.setOrdetailSob1Num(BigDecimal.valueOf(0));
                cOrderDetail1.setOrdetailLastinNum(BigDecimal.valueOf(0));
                cOrderDetail1.setOrdetailSob1RemainNum(BigDecimal.valueOf(0));
                iCOrderDetailService.save(cOrderDetail1);
            }
        } else {
            productsInfo.setPdtinfPdt(pdtInfPdt);
        }
        productsInfo.setPdtinfPrice(BigDecimal.valueOf(pdtInfPrice));
        productsInfo.setPdtinfSubjectId(affiliatedUnit);
        if (pdtInfSpeReadyList != null && !pdtInfSpeReadyList.equals("")) {
            productsInfo.setPdtinfSpeReadylist(pdtInfSpeReadyList.replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        }else {
            productsInfo.setPdtinfSpeReadylist(null);
        }
        if (pdtInfSpePleaseOnCelist != null && !pdtInfSpePleaseOnCelist.equals("")) {
            productsInfo.setPdtinfSpePleaseoncelist(pdtInfSpePleaseOnCelist.replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        }else {
            productsInfo.setPdtinfSpePleaseoncelist(null);
        }
        if (pdtinfSpeSitelist != null && pdtinfSpeSitelist.size() > 0) {
            StringBuilder listBuilder = new StringBuilder();
            for (int i = 0; i < pdtinfSpeSitelist.size(); i++) {
                String s = pdtinfSpeSitelist.get(i);
                QueryWrapper<CAshGateVideoBind> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ID", s);
                CAshGateVideoBind one = iCAshGateVideoBindService.getOne(queryWrapper);
                if (one != null) {
                    listBuilder.append(one.getAshgateName());
                    // 如果不是最后一个元素，则添加逗号
                    if (i < pdtinfSpeSitelist.size() - 1) {
                        listBuilder.append(",");
                    }
                }
            }
            productsInfo.setPdtinfSpeSitelist(listBuilder.toString());
        }else{
            productsInfo.setPdtinfSpeSitelist(null);
        }
        if (pdtinfSpeGateslist != null && !pdtinfSpeGateslist.equals("")) {
            productsInfo.setPdtinfSpeGateslist(pdtinfSpeGateslist.replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        }else {
            productsInfo.setPdtinfSpeGateslist(null);
        }
        if (pdtinfSpeSnlist != null && !pdtinfSpeSnlist.equals("")) {
            productsInfo.setPdtinfSpeSnlist(pdtinfSpeSnlist.replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        }else {
            productsInfo.setPdtinfSpeSnlist(null);
        }
        if (pdtInfStockcls != null) {
            productsInfo.setPdtinfStockcls(pdtInfStockcls);
        }else {
            productsInfo.setPdtinfStockcls(0);
        }
        productsInfo.setPdtinfDeleted(0);
        productsInfo.setPdtinfSpeLoadsite(0);
        productsInfo.setPdtinfLoadCtrl(0);
        productsInfo.setPdtinfIdentyLoad(0);
        productsInfo.setPdtinfPlanCtrl(0);
        productsInfo.setPdtinfQueue(0);
        productsInfo.setPdtinfIdentyQueue(0);
//        QueryWrapper<SysUserPlantConn> queryWrapper4 = new QueryWrapper<>();
//        queryWrapper4.eq("USER_ID", StpUtil.getLoginIdByToken(token).toString());
//        SysUserPlantConn one2 = userPlantConnService.getOne(queryWrapper4);
//        String plantId = one2.getPlantId();
        productsInfo.setDataFrom(plantId);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("新增产品", user.getTrueName(), "新增了产品名称为" + pdtInfPdt + "的产品", plantId);
        productsInfo.setDownedID1(0);
        iCProductsInfoService.save(productsInfo);
        return SzResult.Success();
    }

    //删除品种
    public SzResult deleteProduct(String id, String token, String plantId) throws Exception {
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper);
        if (cProductsInfo == null) {
            return SzResult.error("产品不存在");
        }
        String pdtinfPdt = cProductsInfo.getPdtinfPdt();
        QueryWrapper<CPdtsAuth> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("PDTSAUTH_PDT", pdtinfPdt);
        queryWrapper1.eq("PDTSAUTH_DELETED", 0);
        queryWrapper1.last("limit 1");
        List<CPdtsAuth> list = iCPdtsAuthService.list(queryWrapper1);
        if (list != null && list.size() > 0) {
            return SzResult.error("该品种已被授权，不能删除");
        }

        QueryWrapper<CWeightSheets> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("WEISHT_PRODUCT", pdtinfPdt);
        queryWrapper2.eq("WEISHT_DELETED", 0);
        queryWrapper2.last("limit 1");
        List<CWeightSheets> list1 = iCWeightSheetsService.list(queryWrapper2);
        if (list1 != null && list1.size() > 0) {
            return SzResult.error("该品种已产生磅单，不能删除");
        }
        QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("CARDINF_PREFER_PDT", pdtinfPdt);
        queryWrapper3.eq("CARDINF_DELETED", 0);
        queryWrapper3.last("limit 1");
        List<CIcCardsInfo> list2 = iCIcCardsInfoService.list(queryWrapper3);
        if (list2 != null && list2.size() > 0) {
            return SzResult.error("该品种已绑定IC卡，不能删除");
        }


        cProductsInfo.setPdtinfDeleted(1);
        cProductsInfo.setDownedID1(0);
        iCProductsInfoService.updateById(cProductsInfo);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("删除产品", user.getTrueName(), "删除了产品名称为【" + pdtinfPdt + "】的产品", plantId);
        return SzResult.Success();
    }

    //修改产品
    public SzResult updateProduct(String pdtInfId, String pdtInfClass, String pdtInfPdt, Integer pdtInfPrice, List<String> pdtinfSpeSitelist, List<Integer> pdtinfSpeSnlist, List<Integer> pdtinfSpeGateslist, String affiliatedUnit,
                                  List<Integer> pdtInfSpeReadyList, List<Integer> pdtInfSpePleaseOnCelist, Integer pdtInfStockcls, Integer pdtinfSn ,Integer pdtinfCapacity,String token, String plantId) throws Exception {
        if (pdtInfStockcls == 0) {//出货类
        if (IllegalCharacterCheck.isNameLawful(pdtInfPdt)){
            return SzResult.error("品种名里有非法字符");
        }}else {
            if (IllegalCharacterCheck.isNameLawful2(pdtInfPdt)) {
                return SzResult.error("品种名里有非法字符");
            }
        }
        String nullArray = "[]";
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTINF_ID", pdtInfId);
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.eq("DATA_FROM",plantId);
        CProductsInfo cProductsInfo = iCProductsInfoService.getOne(queryWrapper);
        if (cProductsInfo == null) {
            return SzResult.error("产品不存在");
        }
        String originalPdtInfPdt = cProductsInfo.getPdtinfPdt();//5
        if (pdtInfClass != null && !pdtInfClass.equals("")) {
            cProductsInfo.setPdtinfClass(pdtInfClass);
        }
        if (pdtInfPdt != null && !pdtInfPdt.isEmpty()) {//55
            //不能有特殊字符$#@%&*()_+=|<>?/等
            if ( pdtInfPdt.contains(",")) {
                return SzResult.error("产品名称不能有特殊字符");
            }
            if (pdtInfStockcls == 1) {//进货类
                if (!pdtInfPdt.contains(".")) {
                    pdtInfPdt = "." + pdtInfPdt;
                }
            }else {
                if (pdtInfPdt.contains(".")) {
                    return SzResult.error("产品名称不能有特殊字符");
                }
            }
            //判断有没有重复的
            QueryWrapper<CProductsInfo> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.ne("PDTINF_ID", pdtInfId);
            queryWrapper4.eq("PDTINF_PDT", pdtInfPdt);
            queryWrapper4.eq("PDTINF_DELETED", 0);
            queryWrapper4.eq("DATA_FROM", plantId);
            queryWrapper4.last("limit 1");
            CProductsInfo one = iCProductsInfoService.getOne(queryWrapper4);
            if (one != null) {
                return SzResult.error("产品名称不能重复");
            }
            cProductsInfo.setPdtinfPdt(pdtInfPdt);
            //对应的c_pdts_auth表的PDTSAUTH_PDT字段也要修改
            QueryWrapper<CPdtsAuth> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("PDTSAUTH_PDT", originalPdtInfPdt);
            List<CPdtsAuth> list = iCPdtsAuthService.list(queryWrapper1);
            if (list != null) {
                for (CPdtsAuth cPdtsAuth : list) {
                    cPdtsAuth.setPdtsauthPdt(pdtInfPdt);
                    iCPdtsAuthService.updateById(cPdtsAuth);
                }
            }
            //对应的c_weight_sheets表的WEISHT_PRODUCT字段也要修改
            QueryWrapper<CWeightSheets> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("WEISHT_PRODUCT", originalPdtInfPdt);
            List<CWeightSheets> cWeightSheetsList = iCWeightSheetsService.list(queryWrapper2);
            if (cWeightSheetsList != null) {
                for (CWeightSheets cWeightSheets : cWeightSheetsList) {
                    cWeightSheets.setWeishtProduct(pdtInfPdt);
                    iCWeightSheetsService.updateById(cWeightSheets);
                }
            }
            //对应c_ic_cards_info表的CARDINF_PREFER_PDT字段也要修改
            QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("CARDINF_PREFER_PDT", originalPdtInfPdt);
            List<CIcCardsInfo> cIcCardsInfoList = iCIcCardsInfoService.list(queryWrapper3);
            if (cIcCardsInfoList != null) {
                for (CIcCardsInfo cIcCardsInfo : cIcCardsInfoList) {
                    cIcCardsInfo.setCardinfPreferPdt(pdtInfPdt);
                    iCIcCardsInfoService.updateById(cIcCardsInfo);
                }
            }
        }
        if (pdtInfPrice != null) {
            if (pdtInfPrice < 0) {
                return SzResult.error("品种价格不能小于0");
            }
            cProductsInfo.setPdtinfPrice(BigDecimal.valueOf(pdtInfPrice));
        }
        if (affiliatedUnit != null && !affiliatedUnit.equals("")) {
            cProductsInfo.setPdtinfSubjectId(affiliatedUnit);
        }
        if (pdtinfSn != null) {
            cProductsInfo.setPdtinfSn(new BigDecimal(pdtinfSn));
        }
        if (pdtinfCapacity != null) {
            cProductsInfo.setPdtinfCapacity(pdtinfCapacity);
        }
        if (pdtInfSpeReadyList != null && pdtInfSpeReadyList.size() > 0) {
            cProductsInfo.setPdtinfSpeReadylist(pdtInfSpeReadyList.toString().replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        } else {
            cProductsInfo.setPdtinfSpeReadylist("");
        }
        if (pdtInfSpePleaseOnCelist != null && pdtInfSpePleaseOnCelist.size() > 0) {
            cProductsInfo.setPdtinfSpePleaseoncelist(pdtInfSpePleaseOnCelist.toString().replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        } else {
            cProductsInfo.setPdtinfSpePleaseoncelist("");
        }
        if (pdtinfSpeSitelist != null && pdtinfSpeSitelist.size() > 0) {
            StringBuilder listBuilder = new StringBuilder();
            for (String s : pdtinfSpeSitelist) {
                QueryWrapper<CAshGateVideoBind> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", s);
                CAshGateVideoBind one = iCAshGateVideoBindService.getOne(queryWrapper2);
                if (one != null) {
                    listBuilder.append(one.getAshgateName()).append(",");
                }
            }

            // 移除最后一个逗号
            if (listBuilder.length() > 0) {
                listBuilder.setLength(listBuilder.length() - 1);
            }
            cProductsInfo.setPdtinfSpeSitelist(listBuilder.toString());
        }else {
            cProductsInfo.setPdtinfSpeSitelist("");
        }
        if (pdtinfSpeGateslist != null && pdtinfSpeGateslist.size() > 0) {
            cProductsInfo.setPdtinfSpeGateslist(pdtinfSpeGateslist.toString().replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        } else {
            cProductsInfo.setPdtinfSpeGateslist("");
        }
        if (pdtinfSpeSnlist != null && pdtinfSpeSnlist.size() > 0) {
            cProductsInfo.setPdtinfSpeSnlist(pdtinfSpeSnlist.toString().replace("[", "").replace("]", "").replace("\"", "").replace(" ", ""));
        } else {
            cProductsInfo.setPdtinfSpeSnlist("");
        }
        if (pdtInfStockcls != null && !originalPdtInfPdt.equals(pdtInfPdt)) {
            //判断有没有产生磅单,如果产生磅单了就不可以修改
            QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("WEISHT_PRODUCT", originalPdtInfPdt);
            queryWrapper1.eq("DATA_FROM", plantId);
            queryWrapper1.eq("WEISHT_DELETED", 0);
            queryWrapper1.last("limit 1");
            CWeightSheets cWeightSheets = iCWeightSheetsService.getOne(queryWrapper1);
            if (cWeightSheets != null) {
                return SzResult.error("该产品已经产生磅单,不可以修改名称");
            }
            cProductsInfo.setPdtinfStockcls(pdtInfStockcls);
        }
        cProductsInfo.setDownedID1(0);

        iCProductsInfoService.updateById(cProductsInfo);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("修改产品", user.getTrueName(), "修改了产品名称为【" + pdtInfPdt + "】的产品", plantId);
        return SzResult.Success();
    }


    /**
     * 导出excel
     *
     * @param fields    传入的字段
     * @param filename  文件名
     * @param ids       传入的id
     * @param isHeader  是否有表头
     * @param mode      导出模式
     * @param original  是否原始数据
     * @param pager     分页
     * @param sheetName sheet名
     * @return 返回一个Future
     */
    @Async
    public Future<List<PdtInfExcelEn>> exportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader,
                                                   String mode, Boolean original, Map<String, String> pager, String sheetName, Map<String, String> form, String plantId) throws Exception {
        List<Map<String, Object>> newIds = new ArrayList<>();
        List<PdtInfExcelEn> PdtInfExcelEns = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
                    for (Map<String, Object> newId : newIds) {
                        PdtInfExcelEn pdtInfExcelEn = PdtInfExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("pdtinfPdtClassName")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfClass(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfPdt")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInf(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfPrice")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfPrice(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSubjectName")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSubjectId(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeSitelist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeSnList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeGateslist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeGatesList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeReadylist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeReadyList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpePleaseoncelist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpePleaseOnceList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeLoadsite")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeLoadSite(productDic.pdtLoadSite((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfLoadCtrl")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfLoadCtrl(productDic.pdtLoadCtrl((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfIdentyLoad")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfIdentityLoad(productDic.pdtIdentityLoad((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfPlanCtrl")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfPlanCtrl(productDic.pdtPlanCtrl((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfQueue")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfQueue(productDic.pdtQueue((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfIdentyQueue")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfIdentityQueue(productDic.pdtIdentityQueue((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfStockcls")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfStockCls(productDic.pdtStockCls((Integer) entry.getValue()));
                                }
                            }
                        }
                        PdtInfExcelEns.add(pdtInfExcelEn);
                    }
                    break;
                case "all":

                    String pdtInfPdt = form.get("pdtInfPdt");
                    String pdtInfAlias = form.get("pdtInfAlias");
                    List<CProductsInfo> cPdtInfs = new ArrayList<>();
                    QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like(pdtInfPdt != null && !pdtInfPdt.equals(" "), "PDTINF_PDT", pdtInfPdt);
                    queryWrapper.like(pdtInfAlias != null && !pdtInfAlias.equals(" "), "PDTINF_ALIAS", pdtInfAlias);
                    queryWrapper.eq("DATA_FROM", plantId);
                    queryWrapper.eq("PDTINF_DELETED", 0);
                    cPdtInfs = iCProductsInfoService.list(queryWrapper);
                    for (CProductsInfo cPdtInf : cPdtInfs) {
                        PdtInfExcelEn pdtInfExcelEn = PdtInfExcelEn.builder()
                                .pdtInfPrice(Optional.ofNullable(cPdtInf.getPdtinfPrice()).map(Object::toString).orElse(""))
                                .pdtInfClass(Optional.ofNullable((iCPdtClassService.getById(cPdtInf.getPdtinfClass())).getPdtclassClass()).map(Object::toString).orElse(""))
                                .pdtInf(Optional.ofNullable(cPdtInf.getPdtinfPdt()).orElse(""))
                                .pdtInfSubjectId(Optional.ofNullable((companysService.getById(cPdtInf.getPdtinfSubjectId())).getCpyCo()).orElse(""))
                                .pdtInfSpeSnList(Optional.ofNullable(cPdtInf.getPdtinfSpeSnlist()).orElse(""))
                                .pdtInfSpeGatesList(Optional.ofNullable(cPdtInf.getPdtinfSpeGateslist()).orElse(""))
                                .pdtInfSpeReadyList(Optional.ofNullable(cPdtInf.getPdtinfSpeReadylist()).orElse(""))
                                .pdtInfSpePleaseOnceList(Optional.ofNullable(cPdtInf.getPdtinfSpePleaseoncelist()).orElse(""))
                                .pdtInfSpeLoadSite(Optional.ofNullable(productDic.pdtLoadSite(cPdtInf.getPdtinfSpeLoadsite())).orElse(""))
                                .pdtInfLoadCtrl(Optional.ofNullable(productDic.pdtLoadCtrl(cPdtInf.getPdtinfLoadCtrl())).orElse(""))
                                .pdtInfIdentityLoad(Optional.ofNullable(productDic.pdtIdentityLoad(cPdtInf.getPdtinfIdentyLoad())).orElse(""))
                                .pdtInfPlanCtrl(Optional.ofNullable(productDic.pdtPlanCtrl(cPdtInf.getPdtinfPlanCtrl())).orElse(""))
                                .pdtInfQueue(Optional.ofNullable(productDic.pdtQueue(cPdtInf.getPdtinfQueue())).orElse(""))
                                .pdtInfIdentityQueue(Optional.ofNullable(productDic.pdtIdentityQueue(cPdtInf.getPdtinfIdentyQueue())).orElse(""))
                                .pdtInfStockCls(Optional.ofNullable(productDic.pdtStockCls(cPdtInf.getPdtinfStockcls())).orElse(""))
                                .build();
                        PdtInfExcelEns.add(pdtInfExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
                    for (Map<String, Object> newId : newIds) {
                        PdtInfExcelEn pdtInfExcelEn = PdtInfExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("pdtinfPdtClassName")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfClass(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfPdt")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInf(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfPrice")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfPrice(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSubjectName")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSubjectId(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeSitelist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeSnList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeGateslist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeGatesList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeReadylist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeReadyList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpePleaseoncelist")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpePleaseOnceList(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtinfSpeLoadsite")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfSpeLoadSite(productDic.pdtLoadSite((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfLoadCtrl")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfLoadCtrl(productDic.pdtLoadCtrl((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfIdentyLoad")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfIdentityLoad(productDic.pdtIdentityLoad((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfPlanCtrl")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfPlanCtrl(productDic.pdtPlanCtrl((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfQueue")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfQueue(productDic.pdtQueue((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfIdentyQueue")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfIdentityQueue(productDic.pdtIdentityQueue((Integer) entry.getValue()));
                                }
                            }
                            if (entry.getKey().equals("pdtinfStockcls")) {
                                if (entry.getValue() != "" && entry.getValue() != null) {
                                    pdtInfExcelEn.setPdtInfStockCls(productDic.pdtStockCls((Integer) entry.getValue()));
                                }
                            }
                        }
                        PdtInfExcelEns.add(pdtInfExcelEn);
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(PdtInfExcelEns);
    }


    /**
     * 品种类型导出excel
     *
     * @param fields
     * @param filename
     * @param ids
     * @param isHeader
     * @param mode
     * @param original
     * @param pager
     * @param sheetName
     * @param plantId
     * @param pdtClass
     * @return
     */
    @Async
    public Future<List<PdtClassExcelEn>> pdtClassExportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, boolean isHeader, String mode, Boolean original, Map<String, String> pager, String sheetName, String plantId, String pdtClass) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<PdtClassExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为NoticeExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        PdtClassExcelEn pdtAuthExcelEn = PdtClassExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {

                            if (entry.getKey().equals("pdtclassClass")) {
                                if (entry.getValue() != null) {
                                    pdtAuthExcelEn.setPdtclassClass(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtclassRemark")) {
                                if (entry.getValue() != null) {
                                    pdtAuthExcelEn.setPdtclassRemark(entry.getValue().toString());
                                }
                            }
//                           trucksTrailerno
                            if (entry.getKey().equals("pdtclassFromCtrl")) {
                                if (entry.getValue() != null) {
                                    pdtAuthExcelEn.setPdtclassFromCtrl(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(pdtAuthExcelEn);
                    }
                    break;
                case "all":
                    QueryWrapper<CPdtClass> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like(pdtClass != null && !pdtClass.equals(""), "PDTCLASS_CLASS", pdtClass);
                    queryWrapper.eq("PDTCLASS_DELETED", 0);
                    queryWrapper.eq("DATA_FROM", plantId);
                    List<CPdtClass> cPdtClasses = iCPdtClassService.list(queryWrapper);
                    for (CPdtClass cPdtClass : cPdtClasses) {
                        PdtClassExcelEn pdtAuthExcelEn = PdtClassExcelEn.builder()
                                .pdtclassClass(Optional.ofNullable(cPdtClass.getPdtclassClass()).orElse(""))
                                .pdtclassRemark(Optional.ofNullable(cPdtClass.getPdtclassRemark()).map(Object::toString).orElse(""))
                                .pdtclassFromCtrl(Optional.ofNullable(pdtClassDic.pdtClassFromCtrlDic(cPdtClass.getPdtclassFromCtrl())).map(Object::toString).orElse(""))
                                .build();
                        list.add(pdtAuthExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为NoticeExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        PdtClassExcelEn pdtAuthExcelEn = PdtClassExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {

                            if (entry.getKey().equals("pdtclassClass")) {
                                if (entry.getValue() != null) {
                                    pdtAuthExcelEn.setPdtclassClass(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("pdtclassRemark")) {
                                if (entry.getValue() != null) {
                                    pdtAuthExcelEn.setPdtclassRemark(entry.getValue().toString());
                                }
                            }
//                           trucksTrailerno
                            if (entry.getKey().equals("pdtclassFromCtrl")) {
                                if (entry.getValue() != null) {
                                    pdtAuthExcelEn.setPdtclassFromCtrl(entry.getValue().toString());
                                }
                            }
                        }
                        list.add(pdtAuthExcelEn);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }

    public SzResult getOutProduct(String token, String plantId) {
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
//        plantId="e9f6ed8e091611ec842700163e092c06";
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PDTINF_DELETED", 0);
//        queryWrapper.apply("PDTINF_STOCKCLS=1 OR PDTINF_QUEUE=1");
//        queryWrapper.eq("PDTINF_QUEUE", 1);
        queryWrapper.eq("PDTINF_STOCKCLS", 0);
        List<CProductsInfo> list = iCProductsInfoService.list(queryWrapper);
        return SzResult.success(list);
    }

//    查询所有品种类型为石膏的品种
    public SzResult getGypsumProduct(String pdtName, String plantId) {
        QueryWrapper<CPdtClass> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("PDTCLASS_CLASS","石膏");
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.eq("PDTCLASS_DELETED", 0);
//        getone
        CPdtClass cpdtClass = iCPdtClassService.getOne(queryWrapper1);
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(pdtName != null && !pdtName.equals(""), "PDTINF_PDT", pdtName);
        queryWrapper.eq("PDTINF_CLASS", cpdtClass.getPdtclassId());
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.eq("PDTINF_STOCKCLS", 0);
        List<CProductsInfo> list = iCProductsInfoService.list(queryWrapper);
//        在list存一个id为999的数据
        CProductsInfo cProductsInfo = new CProductsInfo();
        cProductsInfo.setId("999");
        cProductsInfo.setPdtinfId(BigDecimal.valueOf(999));
        cProductsInfo.setPdtinfPdt("全部");
        list.add(cProductsInfo);
        return SzResult.success(list);
    }

    public SzResult getGypsumPlanPdts(String pdtName, String plantId) {
        QueryWrapper<CProductsInfo> productsInfoQueryWrapper = new QueryWrapper<>();
        productsInfoQueryWrapper.or(i -> i.eq("PDTINF_CLASS","2").or().eq("PDTINF_CLASS","3"));
        productsInfoQueryWrapper.like(StringUtils.isNotEmpty(pdtName),"PDTINF_PDT",pdtName);
        productsInfoQueryWrapper.ne("PDTINF_DELETED",1);
        productsInfoQueryWrapper.eq("DATA_FROM",plantId);
        List<Map<String, Object>> list = iCProductsInfoService.list(productsInfoQueryWrapper).stream().map((item) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", item.getId());
            map.put("pdtName", item.getPdtinfPdt());
            return map;
        }).collect(Collectors.toList());
        return SzResult.success(list);
    }

    //    查询所有品种类型为石膏或者炉渣或者湿渣的品种
    public SzResult getGypsumAndZhaProduct(String pdtName, String plantId) {
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(pdtName != null && !pdtName.equals(""), "PDTINF_PDT", pdtName);
        queryWrapper.and(i->i.eq("PDTINF_CLASS","2").or().eq("PDTINF_CLASS","3").or().eq("PDTINF_CLASS","a45ed1623ea342cc8544f8db17eff1fb"));
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.eq("PDTINF_STOCKCLS", 0);
        List<CProductsInfo> list = iCProductsInfoService.list(queryWrapper);
        return SzResult.success(list);
    }
}

